package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Group;
import ge.telasi.tasks.model.TaskReceiver;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.comp.ListModel;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.user.GroupPickDialog;
import ge.telasi.tasks.ui.user.GroupPropertiesDialog;
import ge.telasi.tasks.ui.user.UserPickDialog;
import ge.telasi.tasks.ui.user.UserPropertiesDialog;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

/**
 * @author dimitri
 */
public class TaskReceiverList extends JList {

    public TaskReceiverList() {
        setModel(new ReceiverListModel());
        setCellRenderer(new ReceiverRenderer());
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evnt) {
                TaskReceiverList.this.mouseClicked(evnt);
            }
        });
    }

    private void mouseClicked(MouseEvent evnt) {
        if (evnt.getClickCount() > 1) {
            int index = getSelectedIndex();
            if (index != -1) {
                ReceiverListModel model = (ReceiverListModel) getModel();
                TaskReceiver receiver = model.getObjectAt(index);
                if (receiver.getUser() != null) {
                    UserPropertiesDialog dialog = new UserPropertiesDialog(this);
                    dialog.setUser(receiver.getUser());
                    UIUtils.openDialog(dialog);
                } else {
                    GroupPropertiesDialog dialog = new GroupPropertiesDialog(this);
                    dialog.setGroup(receiver.getGroup());
                    UIUtils.openDialog(dialog);
                }
            }
        }
    }
    
    public void addUser() {
        if (Platform.getDefault().validateConnection()) {
            UserPickDialog dialog = new UserPickDialog(this);
            dialog.refresh();
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                List<User> users = dialog.getSelectedUsers();
                ReceiverListModel model = (ReceiverListModel) getModel();
                for (User user : users) {
                    if (!model.containsUser(user)) {
                        TaskReceiver receiver = new TaskReceiver();
                        receiver.setUser(user);
                        model.add(receiver);
                    }
                }
            }
        }
    }

    public void addGroup() {
        if (Platform.getDefault().validateConnection()) {
            GroupPickDialog dialog = new GroupPickDialog(this);
            dialog.refresh();
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                List<Group> groups = dialog.getSelectedGroups();
                ReceiverListModel model = (ReceiverListModel) getModel();
                for (Group group : groups) {
                    if (!model.containsGroup(group)) {
                        TaskReceiver receiver = new TaskReceiver();
                        receiver.setGroup(group);
                        model.add(receiver);
                    }
                }
            }
        }
    }

    public void removeReceivers() {
        int[] indecies = getSelectedIndices();
        if (indecies != null && indecies.length > 0) {
            ReceiverListModel model = (ReceiverListModel) getModel();
            List<TaskReceiver> receivers = model.getObjectsAt(indecies);
            for (TaskReceiver receiver : receivers) {
                model.remove(receiver);
            }
            clearSelection();
        }
    }

    public void moveUp() {
        int index = getSelectedIndex();
        boolean selected = index != -1;
        boolean first = index == 0;
        boolean canmove = selected && !first;
        if (canmove) {
            int newIndex = index - 1;
            ReceiverListModel model = (ReceiverListModel) getModel();
            TaskReceiver receiver = model.getObjectAt(index);
            model.remove(receiver);
            model.addAt(receiver, newIndex);
            setSelectedIndex(newIndex);
        }
    }

    public void moveDown() {
        int index = getSelectedIndex();
        boolean selected = index != -1;
        boolean last = getModel().getSize() - 1 == index;
        boolean canmove = selected && !last;
        if (canmove) {
            int newIndex = index + 1;
            ReceiverListModel model = (ReceiverListModel) getModel();
            TaskReceiver receiver = model.getObjectAt(index);
            model.remove(receiver);
            model.addAt(receiver, newIndex);
            setSelectedIndex(newIndex);
        }
    }

    public List<TaskReceiver> getReceivers() {
        List<TaskReceiver> receivers = ((ReceiverListModel) getModel()).getData();
        return receivers;
    }

    public void setReceivers(List<TaskReceiver> receivers) {
        ((ReceiverListModel) getModel()).setData(receivers);
    }

    protected class ReceiverListModel extends ListModel<TaskReceiver> {
        boolean containsUser(User user) {
            List<TaskReceiver> receivers = getData();
            for (TaskReceiver receiver : receivers) {
                if (user.equals(receiver.getUser())) {
                    return true;
                }
            }
            return false;
        }

        boolean containsGroup(Group group) {
            List<TaskReceiver> receivers = getData();
            for (TaskReceiver receiver : receivers) {
                if (group.equals(receiver.getGroup())) {
                    return true;
                }
            }
            return false;
        }
    }

    protected class ReceiverRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof TaskReceiver) {
                TaskReceiver receiver = (TaskReceiver) value;
                if (receiver.getUser() != null) {
                    User user = receiver.getUser();
                    setText(user.getFullName() + " <" + user.getUsername() + ">");
                    setIcon(UIUtils.getIcon16x16("user.png"));
                    setToolTipText(user.getFullName());
                } else {
                    Group group = receiver.getGroup();
                    setText(group.getName());
                    setIcon(UIUtils.getIcon16x16("group.png"));
                    setToolTipText(group.getDescription());
                }
            }
            return this;
        }

    }

}
